//
//  UIImage+Extension.swift
//  AispeechMobileApp
//
//  Created by Lee on 2019/2/25.
//  Copyright © 2019年 Lee. All rights reserved.
//

import Foundation
import UIKit

extension UIImage {
    
    //MAEK: 纯色image 可改变alpha
    func halfAlphaBlackImage(imageAlpha:CGFloat , viewColor:UIColor) -> UIImage {
        let imageSize = CGSize(width: 50, height: 50)
        UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale)
        viewColor.withAlphaComponent(imageAlpha).set()
        UIRectFill(CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height))
        let pressesColorImg = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return pressesColorImg!
    }
    
    //改变大小
    func scaleToSize(size:CGSize) -> UIImage {
        
        UIGraphicsBeginImageContext(size)
        self.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return scaledImage!
    }
    
    
    func imageBy(tintColor: UIColor) -> UIImage {
        return byTintColor(tintColor: tintColor, blendMode: .destinationIn)
    }
    
    func byTintColor(tintColor: UIColor,blendMode: CGBlendMode) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0)
        tintColor.setFill()
        let bounds = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        UIRectFill(bounds)
        
        self.draw(in: bounds, blendMode: blendMode, alpha: 1.0)
        if blendMode != .destinationIn {
            self.draw(in: bounds, blendMode: .destinationIn, alpha: 1.0)
        }
        
        let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return tintedImage!
    }
}


